package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.product.pojo.dto.ProductSearchDTO;
import com.xbongbong.pro.product.pojo.dto.ProductSerialDTO;
import com.xbongbong.pro.product.pojo.dto.ProductSerialFieldDTO;
import com.xbongbong.pro.product.pojo.dto.ProductSerialFlowDTO;
import com.xbongbong.pro.product.pojo.dto.ProductStockVerifyDTO;
import com.xbongbong.pro.product.pojo.vo.ProductSearchVO;
import com.xbongbong.pro.product.pojo.vo.ProductSerialFlowVO;
import com.xbongbong.pro.product.pojo.vo.ProductSerialVO;
import com.xbongbong.saas.service.ProductSerialService;
import com.xbongbong.saas.service.ProductService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 产品
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/4/9 11:33
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private ProductSerialService productSerialService;

    /**
     * 产品查询
     *
     * @param productSearchDTO 产品查询入参
     * @param br
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/4/9 13:45
     * @since v1.0
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String search(@RequestBody @Valid ProductSearchDTO productSearchDTO, BindingResult br) {
        XbbResponse<ProductSearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSearchVO productSearchVO = productService.productSearch(productSearchDTO);
                response = new XbbResponse<>(productSearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 产品序列号查重
     * @param productSerialDTO 入参
     * @param br 数据绑定
     * @return String
     */
    @RequestMapping(value = "/repeat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String repeat(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productService.repeat(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/checkStock", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkStock(@RequestBody @Valid ProductStockVerifyDTO productStockVerifyDTO, BindingResult br) {
        XbbResponse<ProductSearchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSearchVO ProductSearchVO = productService.checkStock(productStockVerifyDTO);
                response = new XbbResponse<>(ProductSearchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/getSerialFields", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerialFields(@RequestBody @Valid ProductSerialFieldDTO productSerialFieldDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.getSerialFields(productSerialFieldDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取序列号
     * @param productSerialDTO 入参
     * @param br 参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/getSerial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerial(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.getSerial(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 编辑和详情回显序列号
     * @param productSerialDTO  产品序列号DTO
     * @param br 参数绑定
     * @return
     */
    @RequestMapping(value = "/selectSerial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectSerial(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br){
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.selectSerial(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 序列号跟踪
     * @param productSerialFlowDTO 入参
     * @param br 数据绑定
     * @return 序列号流水记录
     */
    @RequestMapping(value = "/getSerialFlow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerialFlow(@RequestBody @Valid ProductSerialFlowDTO productSerialFlowDTO, BindingResult br){
        XbbResponse<ProductSerialFlowVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialFlowVO productSerialFlowVO = productSerialService.getSerialFlow(productSerialFlowDTO);
                response = new XbbResponse<>(productSerialFlowVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 序列号跟踪
     * @param productSerialDTO 入参
     * @param br 数据绑定
     * @return 序列号流水记录
     */
//    @RequestMapping(value = "/scanSerial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String scanSerial(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br){
//        XbbResponse<ProductSerialVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                ProductSerialVO productSerialVO = productSerialService.scanSerial(productSerialDTO);
//                response = new XbbResponse<>(productSerialVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    /**
     * 出库批量录入判断序列号是否在库
     * @param productSerialDTO 入参
     * @param br 参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/checkSerialStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkSerialStatus(@RequestBody @Valid ProductSerialDTO productSerialDTO, BindingResult br) {
        XbbResponse<ProductSerialVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductSerialVO productSerialVO = productSerialService.checkSerialStatus(productSerialDTO);
                response = new XbbResponse<>(productSerialVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
